//
//  UPActivityTopView.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/20.
//

#import "UPActivityTopView.h"
#import "LeaderibleModel.h"

@implementation UPActivityTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInitSubviews];
    }
    return self;
}

- (void)commonInitSubviews {
    [self addSubview:self.contentImgView];
    [self addSubview:self.maxLoanImgView];
    [self addSubview:self.daysImgView];
    [self addSubview:self.rateImgView];
    [self addSubview:self.toLoanButton];
    [self addSubview:self.daysLabel];
    [self addSubview:self.rateLabel];
    [self addSubview:self.amountMoneyLabel];
    [self addSubview:self.detailLabel];
    [self setMasnoary];
    
    [self addTarget:self action:@selector(clickTopView) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setMasnoary {

    CGFloat width = kScreenWidth - 2 * 16.0;
    CGFloat height = [self getTopContentImageHeightWithWidth:width];
    [self.contentImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(height);
        make.top.equalTo(self).inset(20);
    }];
    
    UIImage *image = nil;
    if ([AppConfig sharedInstance].appCountryEnv == AppCountryEnvIndonesia) {
        image = [UIImage imageNamed:@"max_loan_count_id"];
    } else {
        image = [UIImage imageNamed:@"max_loan_count"];
    }
    CGFloat aspectRatio1 = 1.0;
    if (image.size.width > 0 && image.size.height > 0) {
        aspectRatio1 = image.size.height / image.size.width;
    }
    CGFloat loanHeight = 50.0;
    CGFloat loanWith = loanHeight / aspectRatio1;
    [self.maxLoanImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentImgView).offset(-10);
        make.top.equalTo(self);
        make.height.mas_equalTo(loanHeight);
        make.width.mas_equalTo(loanWith);
    }];
    
    [self.daysImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentImgView).offset(-45);
        make.top.equalTo(self).offset(5);
        make.height.mas_equalTo(43);
        make.width.mas_equalTo(69);
    }];
    
    [self.daysLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.daysImgView);
    }];
    
    [self.rateImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentImgView).offset(20);
        make.top.equalTo(self).offset(20);
        make.height.mas_equalTo(63);
        make.width.mas_equalTo(82);
    }];
    
    [self.rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.rateImgView);
    }];
    
    [self.amountMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.detailLabel.mas_top).offset(-2 * kScale);
        make.centerX.equalTo(self.contentImgView);
        make.top.equalTo(self.maxLoanImgView.mas_bottom).offset(3);
    }];
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.toLoanButton.mas_top).offset(-8 * kScale);
        make.centerX.equalTo(self.contentImgView);
        make.height.mas_equalTo(20);
    }];
    
    UIImage *buttonImage = [UIImage imageNamed:@"button_bg_style1"];
    CGFloat buttonHeight = 60.0 * kScale;
    CGFloat buttonAspectRatio = buttonImage.size.width / buttonImage.size.height;
    CGFloat buttonWidth = buttonHeight * buttonAspectRatio;
    [self.toLoanButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-35);
        make.centerX.equalTo(self);
        make.height.mas_equalTo(buttonHeight);
        make.width.mas_equalTo(buttonWidth);
    }];
}

- (void)setCircumibility:(Circumibility *)circumibility {
    _circumibility = circumibility;
    LeaderibleModel *card = circumibility.leaderible.firstObject;
    self.daysLabel.text = card.fidemost;
    self.rateLabel.text = card.crattion;
    self.amountMoneyLabel.text = card.buyation;
}

- (UIImageView *)maxLoanImgView {
    if (!_maxLoanImgView) {
        _maxLoanImgView = [[UIImageView alloc] init];
        _maxLoanImgView.contentMode = UIViewContentModeScaleAspectFit;
        if ([AppConfig sharedInstance].appCountryEnv == AppCountryEnvIndonesia) {
            _maxLoanImgView.image = [UIImage imageNamed:@"max_loan_count_id"];
        } else {
            _maxLoanImgView.image = [UIImage imageNamed:@"max_loan_count"];
        }
    }
    return _maxLoanImgView;
};

- (UIImageView *)contentImgView {
    if (!_contentImgView) {
        _contentImgView = [[UIImageView alloc] init];
        _contentImgView.image = [UIImage imageNamed:@"home_top_content_bg"];
        _contentImgView.contentMode = UIViewContentModeScaleAspectFit;
        _contentImgView.clipsToBounds = true;
    }
    return _contentImgView;
};

- (UIImageView *)daysImgView {
    if (!_daysImgView) {
        _daysImgView = [[UIImageView alloc] init];
        _daysImgView.image = [UIImage imageNamed:@"days_bg"];
        _daysImgView.contentMode = UIViewContentModeScaleAspectFit;
        _daysImgView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI / 16);
    }
    return _daysImgView;
};

- (UIImageView *)rateImgView {
    if (!_rateImgView) {
        _rateImgView = [[UIImageView alloc] init];
        _rateImgView.image = [UIImage imageNamed:@"rate_bg"];
        _rateImgView.contentMode = UIViewContentModeScaleAspectFit;
        _rateImgView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI / 16);
    }
    return _rateImgView;
};

- (UIButton *)toLoanButton {
    if (!_toLoanButton) {
        _toLoanButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_toLoanButton setBackgroundImage:[UIImage imageNamed:@"button_bg_style1"] forState: UIControlStateNormal];
        [_toLoanButton setTitle:LocalizableForKey(@"Go to loan") forState:UIControlStateNormal];
        [_toLoanButton setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal];
        UIFont *italicFont = [UIFont italicSystemFontOfSize:15];
        UIFontDescriptor *boldItalicDescriptor = [italicFont.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic];
        _toLoanButton.titleLabel.font = [UIFont fontWithDescriptor:boldItalicDescriptor size:15];
        [_toLoanButton addTarget:self action:@selector(clickTopView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _toLoanButton;
}

- (UILabel *)daysLabel {
    if (!_daysLabel) {
        _daysLabel = [[UILabel alloc] init];
        _daysLabel.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI / 16);
        _daysLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightSemibold];
        _daysLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _daysLabel;
}

- (UILabel *)rateLabel {
    if (!_rateLabel) {
        _rateLabel = [[UILabel alloc] init];
        _rateLabel.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI / 16);
        _rateLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightSemibold];
        _rateLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _rateLabel;
}

- (UILabel *)amountMoneyLabel {
    if (!_amountMoneyLabel) {
        _amountMoneyLabel = [[UILabel alloc] init];
        _amountMoneyLabel.font = [UIFont boldSystemFontOfSize:47];
        _amountMoneyLabel.textAlignment = NSTextAlignmentCenter;
        _amountMoneyLabel.textColor = [UIColor colorWithHexString:@"#000000"];
        _amountMoneyLabel.adjustsFontSizeToFitWidth = YES;
    }
    return _amountMoneyLabel;
}

- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.textColor = [[UIColor colorWithHexString:@"#000000"] colorWithAlphaComponent: 0.4];
        _detailLabel.text = LocalizableForKey(@"Fast approval, no need to wait");
        _detailLabel.adjustsFontSizeToFitWidth = YES;
    }
    return _detailLabel;
}

- (void)clickTopView {
    if ([self.delegate respondsToSelector:@selector(activityTopViewDidClick)]) {
        [self.delegate activityTopViewDidClick];
    }
}

- (CGFloat)getTopContentImageHeightWithWidth:(CGFloat)width {
    UIImage *image = [UIImage imageNamed:@"home_top_content_bg"];
    CGFloat quickRatio = 1.0;
    if (image.size.width > 0 && image.size.height > 0) {
        quickRatio = image.size.width / image.size.height;
    }
    
    CGFloat height = width / quickRatio;
    return height;
}

- (CGFloat)getTopViewHeight {
    return [self getTopContentImageHeightWithWidth:kScreenWidth - 2 * 16.0] + 20;
}

@end
